<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="lib/jquery.js"></script>
  </head>
  <body>
    <!-- 文件选择框 -->
    <input type="file" id="file1" />
    <!-- 上传文件的按钮 -->
    <button id="btnUpload">上传文件</button>
    <br />
    <img src="images/loading.gif" alt="" style="display: none" id="loading" />
    <script>
      $(function () {
        // 监听到AJAX的请求被发起
        $(document).ajaxStart(function () {
          $("#loading").show();
        });
        // 监听到AJAX请求结束后
        $(document).ajaxStop(function () {
          $("#loading").hide();
        });
        $("#btnUpload").on("click", function () {
          let files = $("#file1")[0].files;
          console.log(files);
          if (files.length <= 0) {
            return alert("请上传文件");
          }
          let fd = new FormData();
          fd.append("avatar", files[0]);
          $.ajax({
            method: "POST",
            url: "http://www.liulongbin.top:3006/api/upload/avatar",
            data: fd,
            processData: false,
            contentType: false,
            success(res) {
              console.log(res);
            },
          });
        });
      });
    </script>
  </body>
</html>
